package com.hera.base.web.vaadin.mantenimiento;



import static com.hera.base.util.InputData.getStringData;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.FiltroDTO;
import com.hera.base.util.TableCustom;
import com.hera.base.web.BaseUI;
import com.hera.base.web.MantenimientoMaestroDetalle;
import com.hera.process.config.domain.BusinessDay;
import com.hera.process.config.domain.BusinessHour;
import com.hera.process.config.domain.BusinessHourPK;
import com.hera.process.config.domain.ParameterDetail;
import com.hera.process.config.rest.delegate.ConfigDelegate;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class BussinesUI extends BaseUI implements MantenimientoMaestroDetalle,ClickListener{

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	/**
	 * 
	 */
	private static final long serialVersionUID = -7352647374236503560L;
	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_2;
	@AutoGenerated
	private HorizontalLayout lytMaestro;
	@AutoGenerated
	private VerticalLayout pnlFormulariod;
	@AutoGenerated
	private HorizontalLayout hlytBotonera;
	@AutoGenerated
	private Button btnEliminarMaestro;
	@AutoGenerated
	private Button btnGuardarMaestro;
	@AutoGenerated
	private TableCustom tblfListDay;
	@AutoGenerated
	private VerticalLayout verticalLayout_4;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;
	@AutoGenerated
	private Button btnQuitar;
	@AutoGenerated
	private Button btnAgregar;
	@AutoGenerated
	private TextField txtFinHora;
	@AutoGenerated
	private TextField txtInicioHora;
	@AutoGenerated
	private ComboBox cmbEstadoMaestro;
	@AutoGenerated
	private Label label_1;
	@AutoGenerated
	private HorizontalLayout lytTablaMaestra;
	@AutoGenerated
	private TableCustom tblfMaestro;
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	private static final Logger log = Logger.getLogger(BussinesUI.class);
	
	public BussinesUI() {
		buildMainLayout();
		setCompositionRoot(mainLayout);

		// TODO add user code here
	}
	
	
	@Autowired
	ConfigDelegate configdelegate;
	boolean esNuevo;
	boolean esNuevoDetalle;
	
	BusinessDay bussinessday;
	BusinessHour businesshour;
	Long idCalendar;
	


	@Override
	public void cargarFormulario() {
		
		esNuevo=true;
		bussinessday=null;
		
		
		tblfMaestro.setTitulo("Listado de Dias Laborables");
		tblfMaestro.setHeight("200px");
		
		tblfMaestro.addColumn("day.name", "Dia", 150, true, String.class);
		
		tblfListDay.setHeight("210px");
		tblfListDay.setTitulo("");
		tblfListDay.addColumn("businessHourPK.startHour", "Inicio", 60, true, String.class);
		tblfListDay.addColumn("endHour", "Fin", 60, true,String.class);
		
		cmbEstadoMaestro.setReadOnly(false);
		
		btnAgregar.addClickListener(this);
		
		tblfMaestro.construir();
		
		tblfListDay.construir();
		
		
		
		
		tblfMaestro.addTextChangeListener(new TextChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void textChange(TextChangeEvent event) {
				buscarMaestro(event);
				
			}
		});
		
		tblfMaestro.addValueChangeListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				bussinessday=(BusinessDay)tblfMaestro.getSelectedItem(event);
				esNuevo=(bussinessday==null);
				if (esNuevo) {
					limpiarMaestro();
		
				}else {
				
				//	cmbEstadoMaestro.setValue(businessday.getDay().getName().toString());
					
					cmbEstadoMaestro.setValue(bussinessday.getDay());			

					
					
					
					
					FiltroDTO filtroDTO=new FiltroDTO();					
					filtroDTO.addParametro("businessHourPK.idCalendar", bussinessday.getBusinessDayPK().getIdCalendar());
					filtroDTO.addParametro("businessHourPK.idBusinessDay", bussinessday.getBusinessDayPK().getIdBusinessDay());
					List<BusinessHour> lstBussinesOur=configdelegate.findAllBusinessHours(filtroDTO);
					bussinessday.setBusinessHourList(lstBussinesOur);
					tblfListDay.setLstData(lstBussinesOur);
					
					
					tblfListDay.addTextChangeListener(new TextChangeListener() {
						private static final long serialVersionUID = 1L;

						@Override
						public void textChange(TextChangeEvent event) {
							buscarMaestro(event);
							
						}
					});
					
					tblfListDay.addValueChangeListener(new ValueChangeListener() {
						
						/**
						 * 
						 */
						private static final long serialVersionUID = 1L;

						@Override
						public void valueChange(ValueChangeEvent event) {
							
						businesshour=(BusinessHour)tblfListDay.getSelectedItem(event);
						esNuevo=(businesshour==null);
						tblfListDay.limpiarFiltro();
						
						if (esNuevo) {
							limpiarMaestro();
						}else {
							
							btnEliminarMaestro.setVisible(true);
							btnGuardarMaestro.setCaption("Modificar");
							
							txtInicioHora.setValue(businesshour.getBusinessHourPK().getStartHour());
							txtFinHora.setValue(businesshour.getEndHour());
							
							
						}
							
							
						}
					});
					
					
		
				}
				
			}
		});
		
		
		
		
		List<ParameterDetail>lstDay=configdelegate.findDays();
		BeanItemContainer<ParameterDetail>container1=new BeanItemContainer<ParameterDetail>(ParameterDetail.class);
		container1.addAll(lstDay);
		cmbEstadoMaestro.setContainerDataSource(container1);
		cmbEstadoMaestro.setItemCaptionPropertyId("name");
		
		
		
	

		
		
		
	}

	@Override
	public void buscarMaestro() {
		buscarMaestro(null);
		
	}

	@Override
	public void buscarMaestro(TextChangeEvent event) {
		
		
		mostrarData(idCalendar);
	}

	@Override
	public void limpiarMaestro() {
		log.info("LIMPIAR MAESTRO ");
		btnEliminarMaestro.setVisible(false);
		btnGuardarMaestro.setCaption("Nuevo");
		cmbEstadoMaestro.setValue(null);
		txtInicioHora.setValue("");
		txtFinHora.setValue("");
		
		
		log.info("fin limpiarMaestro");
		
	}

	@Override
	public void eliminarMaestro() {
	
			log.info("inicio eliminarMaestro");
			configdelegate.deleteBusinessDay(bussinessday);
			buscarMaestro();
			limpiarMaestro();
			log.info("fin eliminarMaestro");
		
	}

	@Override
	public void guardarMaestro() {
		
		log.info("inicio guardarMaestro");
		if(esNuevo){
			bussinessday=new BusinessDay();
			
			configdelegate.createBusinessDay(bussinessday);
			
		}else {
			configdelegate.updateBusinessDay(bussinessday);
		}
		
		
	}

	@Override
	public void buscarDetalle() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void buscarDetalle(TextChangeEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void limpiarDetalle() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eliminarDetalle() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void guardarDetalle() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void postConstruct() {
		cargarFormulario();
		buscarMaestro();
		
	}

	@Override
	public void buttonClick(ClickEvent event) {
		if (event.getButton().equals(btnAgregar)) {
			List<BusinessHour> rangosNew = new ArrayList<BusinessHour>();
			List<BusinessHour> rangosAnt = bussinessday.getBusinessHourList();
			
			for (BusinessHour businessHour : rangosAnt) {
				rangosNew.add(businessHour);
			}
			
			BusinessHour businesshour1 = new BusinessHour();
			businesshour1.setBusinessHourPK(new BusinessHourPK());
			businesshour1.setEndHour("5");

			businesshour1.getBusinessHourPK().setIdBusinessDay(bussinessday.getBusinessDayPK().getIdBusinessDay());
			businesshour1.getBusinessHourPK().setIdCalendar(bussinessday.getBusinessDayPK().getIdCalendar());
			
			businesshour1.getBusinessHourPK().setStartHour("8");
			
			bussinessday.setBusinessHourList(rangosNew);

			tblfListDay.setLstData(rangosNew);
			
			
		}else if(event.getButton().equals(btnEliminarMaestro)){
			eliminarMaestro();
		}else if(event.getButton().equals(btnGuardarMaestro)){
			guardarMaestro();
		}
		
	}
	
	private void obtenerDatosFormulario(BusinessHour businesshour) {
	
		
		
		businesshour.getBusinessHourPK().setStartHour(businesshour.getBusinessHourPK().getStartHour());
		businesshour.setEndHour(getStringData(txtFinHora,"Fin"));
		
		
		
		
	}

	public void mostrarData(Long idCalendar){
		this.idCalendar=idCalendar;
		log.info("inicio buscarMaestro");
		FiltroDTO filtroDTO=new FiltroDTO();
		if (idCalendar != null){
			filtroDTO.addParametro("businessDayPK.idCalendar",idCalendar);
			List<BusinessDay> lstBussinesDay=configdelegate.findAllBusinessDay(filtroDTO);
			tblfMaestro.setLstData(lstBussinesDay);
			
			log.info("fin buscarMaestro");
		}else {
			tblfMaestro.setLstData(new ArrayList<BusinessDay>());
		}
		
		limpiarMaestro();

		
		
		
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("-1px");
		mainLayout.setHeight("-1px");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("-1px");
		setHeight("-1px");
		
		// verticalLayout_2
		verticalLayout_2 = buildVerticalLayout_2();
		mainLayout.addComponent(verticalLayout_2);
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_2() {
		// common part: create layout
		verticalLayout_2 = new VerticalLayout();
		verticalLayout_2.setImmediate(false);
		verticalLayout_2.setWidth("-1px");
		verticalLayout_2.setHeight("-1px");
		verticalLayout_2.setMargin(false);
		
		// lytMaestro
		lytMaestro = buildLytMaestro();
		verticalLayout_2.addComponent(lytMaestro);
		
		return verticalLayout_2;
	}

	@AutoGenerated
	private HorizontalLayout buildLytMaestro() {
		// common part: create layout
		lytMaestro = new HorizontalLayout();
		lytMaestro.setImmediate(false);
		lytMaestro.setWidth("-1px");
		lytMaestro.setHeight("-1px");
		lytMaestro.setMargin(false);
		
		// lytTablaMaestra
		lytTablaMaestra = buildLytTablaMaestra();
		lytMaestro.addComponent(lytTablaMaestra);
		
		// pnlFormulariod
		pnlFormulariod = buildPnlFormulariod();
		lytMaestro.addComponent(pnlFormulariod);
		
		return lytMaestro;
	}

	@AutoGenerated
	private HorizontalLayout buildLytTablaMaestra() {
		// common part: create layout
		lytTablaMaestra = new HorizontalLayout();
		lytTablaMaestra.setImmediate(false);
		lytTablaMaestra.setWidth("400px");
		lytTablaMaestra.setHeight("-1px");
		lytTablaMaestra.setMargin(false);
		
		// tblfMaestro
		tblfMaestro = new TableCustom();
		tblfMaestro.setImmediate(false);
		tblfMaestro.setWidth("-1px");
		tblfMaestro.setHeight("-1px");
		lytTablaMaestra.addComponent(tblfMaestro);
		
		return lytTablaMaestra;
	}

	@AutoGenerated
	private VerticalLayout buildPnlFormulariod() {
		// common part: create layout
		pnlFormulariod = new VerticalLayout();
		pnlFormulariod.setImmediate(false);
		pnlFormulariod.setWidth("-1px");
		pnlFormulariod.setHeight("-1px");
		pnlFormulariod.setMargin(false);
		pnlFormulariod.setSpacing(true);
		
		// label_1
		label_1 = new Label();
		label_1.setStyleName("titulo");
		label_1.setImmediate(false);
		label_1.setWidth("-1px");
		label_1.setHeight("-1px");
		label_1.setValue("BussniesDay");
		pnlFormulariod.addComponent(label_1);
		pnlFormulariod.setComponentAlignment(label_1, new Alignment(20));
		
		// cmbEstadoMaestro
		cmbEstadoMaestro = new ComboBox();
		cmbEstadoMaestro.setStyleName("fieldRequired");
		cmbEstadoMaestro.setImmediate(false);
		cmbEstadoMaestro.setDescription("Estado");
		cmbEstadoMaestro.setWidth("200px");
		cmbEstadoMaestro.setHeight("-1px");
		cmbEstadoMaestro.setTabIndex(1);
		cmbEstadoMaestro.setInputPrompt("Estado");
		pnlFormulariod.addComponent(cmbEstadoMaestro);
		
		// verticalLayout_4
		verticalLayout_4 = buildVerticalLayout_4();
		pnlFormulariod.addComponent(verticalLayout_4);
		
		// tblfListDay
		tblfListDay = new TableCustom();
		tblfListDay.setImmediate(false);
		tblfListDay.setWidth("-1px");
		tblfListDay.setHeight("-1px");
		pnlFormulariod.addComponent(tblfListDay);
		
		// hlytBotonera
		hlytBotonera = buildHlytBotonera();
		pnlFormulariod.addComponent(hlytBotonera);
		pnlFormulariod.setComponentAlignment(hlytBotonera, new Alignment(20));
		
		return pnlFormulariod;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_4() {
		// common part: create layout
		verticalLayout_4 = new VerticalLayout();
		verticalLayout_4.setImmediate(false);
		verticalLayout_4.setWidth("-1px");
		verticalLayout_4.setHeight("-1px");
		verticalLayout_4.setMargin(false);
		
		// txtInicioHora
		txtInicioHora = new TextField();
		txtInicioHora.setImmediate(false);
		txtInicioHora.setDescription("Inicio");
		txtInicioHora.setWidth("-1px");
		txtInicioHora.setHeight("-1px");
		txtInicioHora.setInputPrompt("Inicio");
		verticalLayout_4.addComponent(txtInicioHora);
		
		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		verticalLayout_4.addComponent(horizontalLayout_1);
		
		return verticalLayout_4;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("-1px");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(false);
		horizontalLayout_1.setSpacing(true);
		
		// txtFinHora
		txtFinHora = new TextField();
		txtFinHora.setImmediate(false);
		txtFinHora.setDescription("Fin");
		txtFinHora.setWidth("-1px");
		txtFinHora.setHeight("-1px");
		txtFinHora.setInputPrompt("Fin");
		horizontalLayout_1.addComponent(txtFinHora);
		
		// btnAgregar
		btnAgregar = new Button();
		btnAgregar.setCaption("  +");
		btnAgregar.setImmediate(true);
		btnAgregar.setWidth("-1px");
		btnAgregar.setHeight("-1px");
		horizontalLayout_1.addComponent(btnAgregar);
		
		// btnQuitar
		btnQuitar = new Button();
		btnQuitar.setCaption("  -");
		btnQuitar.setImmediate(true);
		btnQuitar.setWidth("-1px");
		btnQuitar.setHeight("-1px");
		horizontalLayout_1.addComponent(btnQuitar);
		
		return horizontalLayout_1;
	}

	@AutoGenerated
	private HorizontalLayout buildHlytBotonera() {
		// common part: create layout
		hlytBotonera = new HorizontalLayout();
		hlytBotonera.setImmediate(false);
		hlytBotonera.setWidth("-1px");
		hlytBotonera.setHeight("-1px");
		hlytBotonera.setMargin(false);
		hlytBotonera.setSpacing(true);
		
		// btnGuardarMaestro
		btnGuardarMaestro = new Button();
		btnGuardarMaestro.setCaption("Guardar");
		btnGuardarMaestro.setImmediate(true);
		btnGuardarMaestro.setWidth("-1px");
		btnGuardarMaestro.setHeight("-1px");
		hlytBotonera.addComponent(btnGuardarMaestro);
		
		// btnEliminarMaestro
		btnEliminarMaestro = new Button();
		btnEliminarMaestro.setCaption("Eliminar");
		btnEliminarMaestro.setImmediate(true);
		btnEliminarMaestro.setWidth("-1px");
		btnEliminarMaestro.setHeight("-1px");
		hlytBotonera.addComponent(btnEliminarMaestro);
		
		return hlytBotonera;
	}
}
